<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once(dirname(__FILE__) . "/../common/Logger.php");
require_once(dirname(__FILE__) . "/../common/Common.php");
require_once(dirname(__FILE__) . "/../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../dbcom/MachineCom.php");
require_once(dirname(__FILE__) . "/../dbcom/WorkerCom.php");

$log = new Logger("./../machine.log");
$common = new Common();
$ragioncom = new RagionCom();
$machinecom = new MachineCom();
$workercom = new WorkerCom();

if (!isset($_REQUEST['cont']) || empty($_REQUEST['cont'])) {
    echo json_encode(array("code" => 100, "msg" => "cannot request cont"));
    exit();
}

switch ($_REQUEST['cont']){
    case 1://machine list
        if (!isset($_REQUEST['userid']) || empty($_REQUEST['userid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot get userid"));
            exit();
        }
        $workerid = $_REQUEST['userid'];
        $worker = $workercom->getUserById($workerid);
        if (!$worker) {
            echo json_encode(array("code" => 400, "msg" => "cannot get workerinfo"));
            exit();
        }
        $machinelist = $machinecom->getMachineByComid($worker['community_id']);
        if(!$machinelist){
            echo json_encode(array("code" => 400, "msg" => "cannot get machinelist"));
            exit();
        }
        if(empty($machinelist)){
            echo json_encode(array("code" => 0, "msg" => "success","data"=>""));
            exit();
        }
        $result = array("code" => 0, "msg" => "success", "data" => array());
        foreach($machinelist as $k=>$v){
            $result["data"][$k]['id'] = $v['id'];
            $result["data"][$k]['name'] = $v['des'];
            $result["data"][$k]['status'] = $v['status'];
            $community = $ragioncom->getRagionById($v['community_id']);
            $result["data"][$k]['community'] = $community['name'];
        }
        echo json_encode($result);
        break;
    case 2://启用
        if (!isset($_REQUEST['machineid']) || empty($_REQUEST['machineid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot get machineid"));
            exit();
        }
        $machine_id = $_REQUEST['machineid'];
        $res = $machinecom->changeStatus($machine_id, 1);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "change machine status failed"));
        }
        break;
    case 3://停用
        if (!isset($_REQUEST['machineid']) || empty($_REQUEST['machineid'])) {
            echo json_encode(array("code" => 100, "msg" => "cannot get machineid"));
            exit();
        }
        $machine_id = $_REQUEST['machineid'];
        $res = $machinecom->changeStatus($machine_id, 2);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "change machine status failed"));
        }
        break;
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined failed"));
        break;
}

exit();